package com.qiezi.util;

import android.app.Activity;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.Window;

public class SoftKeyBoardListener {

    private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
    private View rootView;
    int rootViewVisibleHeight;

    public SoftKeyBoardListener(View view) {
        this.rootView = view;
        setListener();
    }

    public SoftKeyBoardListener(Activity paramActivity) {
        this.rootView = paramActivity.getWindow().getDecorView();
        setListener();
    }

    public void setListener(){

        this.rootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                Rect localRect = new Rect();
                SoftKeyBoardListener.this.rootView.getWindowVisibleDisplayFrame(localRect);
                int i = localRect.height();
                if (SoftKeyBoardListener.this.rootViewVisibleHeight == 0) {
                    SoftKeyBoardListener.this.rootViewVisibleHeight = i;
                    return;
                }
                if (SoftKeyBoardListener.this.rootViewVisibleHeight == i) {
                    return;
                }
                if (SoftKeyBoardListener.this.rootViewVisibleHeight - i > 200) {
                    if (SoftKeyBoardListener.this.onSoftKeyBoardChangeListener != null) {
                        SoftKeyBoardListener.this.onSoftKeyBoardChangeListener.keyBoardShow(SoftKeyBoardListener.this.rootViewVisibleHeight - i);
                    }
                    SoftKeyBoardListener.this.rootViewVisibleHeight = i;
                    return;
                }
                if (i - SoftKeyBoardListener.this.rootViewVisibleHeight > 200) {
                    if (SoftKeyBoardListener.this.onSoftKeyBoardChangeListener != null) {
                        SoftKeyBoardListener.this.onSoftKeyBoardChangeListener.keyBoardHide(i - SoftKeyBoardListener.this.rootViewVisibleHeight);
                    }
                    SoftKeyBoardListener.this.rootViewVisibleHeight = i;
                    return;
                }
            }
        });
    }

    public static void setListener(View decorView, OnSoftKeyBoardChangeListener paramOnSoftKeyBoardChangeListener) {
        new SoftKeyBoardListener(decorView).setOnSoftKeyBoardChangeListener(paramOnSoftKeyBoardChangeListener);
    }

    public static void setListener(Activity paramActivity, OnSoftKeyBoardChangeListener paramOnSoftKeyBoardChangeListener) {
        new SoftKeyBoardListener(paramActivity).setOnSoftKeyBoardChangeListener(paramOnSoftKeyBoardChangeListener);
    }

    private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener paramOnSoftKeyBoardChangeListener) {
        this.onSoftKeyBoardChangeListener = paramOnSoftKeyBoardChangeListener;
    }

    public interface OnSoftKeyBoardChangeListener {
        void keyBoardHide(int paramInt);

        void keyBoardShow(int paramInt);
    }
}
